<?php

namespace app\model;

use think\Model;

class Technical extends Model
{


    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'technical';

    public static function getTitle2($uuid)
    {
        $data = self::where('id', $uuid)->find();
        return $data->title;
    }

    public static function getTitle($uuid)
    {
        $data = self::where('uuid', $uuid)->find();
        return $data->title;
    }

    public static function getcityName($uuid)
    {
        $data = self::where('uuid', $uuid)->find();
        return $data->city_name;
    }

    public static function getTel($uuid)
    {
        $data = self::where('uuid', $uuid)->find();
        return $data->tel;
    }

    public static function getInfo($uuid)
    {
        $data = self::where('uuid', $uuid)->find();
        if (!empty($data)) {
            $data = $data->toArray();
        }
        return $data;
    }

    public static function getpcarray($bail = false)
    {
        $query =  self::field('uuid,title')->where(['uniacid' => uniacid(), 'status' => 1]);

        if($bail){
            $bailConfig = Config::getconfig('bail');
            if($bailConfig['is_technical_bail'] == 1) {
                $query->where('bail','>=',$bailConfig['is_technical_bail_min_amount']);
            }
        }

        $data = $query->select()->toArray();
        $datalist = [];
        foreach ($data as $key => $vo) {
            if (!empty($vo['uuid'])) {
                $datalist[$key]['val'] = $vo['uuid'];
                $datalist[$key]['key'] = $vo['title'];
            }
        }
        return $datalist;
    }

    public static function gelist_bycity($city_name)
    {
        $where['uniacid'] = uniacid();
        $where['status'] = 1;
        $where['city_name'] = $city_name;

        $query = Technical::where($where);

        $Sort = 'id desc';

        $data = $query->order($Sort)->select()->toArray();

        return $data;
    }

    public static function getdiy_bycat($cat = 0, $count = 4, $goodsSort = "all")
    {
        $where['uniacid'] = uniacid();
        $where['status'] = 1;

        if ($cat > 0) {
            if (!empty($cat)) {
                $where['category_id'] = Category::getsonid($cat);
            }
        }

        $query = Technical::where($where);

        $Sort = 'id desc';

        $data = $query->limit($count)
            ->order($Sort)->select()->toArray();

        return $data;
    }

    public static function setIncome($order_info)
    {
        $uuid = OrderStaff::getuuid($order_info['id']);
        if (!empty($uuid)) {
            $Technical = Technical::where(['uuid' => $uuid])->find();
            if (!empty($Technical)) {
                $Technical = $Technical->toArray();
            }

            if (!empty($Technical['status'])) {
                $percent = TechnicalLevel::getPercent($Technical['level']);
                if (empty($percent)) {
                    $percent = Config::getconfig('technical')['return_percent'];
                }
                //如果是商户的师傅，分佣比较需要减去商户的佣金
                if (!empty($Technical['sid'])) {
                    $Store = Store::find($Technical['sid']);
                    if (!empty($Store)) {
                        $Store = $Store->toArray();
                        $storepercent = StoreCate::getPercent($Store['stid']);
                        if (empty($storepercent)) {
                            $storepercent = Config::getconfig('store')['return_percent'];
                        }
                        if (!empty($storepercent)) {
                            $percent = $percent - $storepercent;
                        }
                    }
                }

                if ((int)$percent > 0) {

                    $income = ($order_info['total'] * percent_to_num($percent));
                    $return_percent = $percent;

                    if ($income > 0 && $income < 0.01) {
                        $income = 0.01;
                    }
                    Technical::where('uuid', $uuid)->inc('income', $income)->update();
                    Technical::where('uuid', $uuid)->inc('total_income', $income)->update();
                    Technical::where('uuid', $uuid)->inc('service_times', 1)->update();

                    $incomedata['uuid'] = $uuid;
                    $incomedata['ptype'] = 1;
                    $incomedata['uniacid'] = uniacid();
                    $incomedata['order_id'] = $order_info['id'];
                    $incomedata['order_num_alias'] = $order_info['order_num_alias'];
                    $incomedata['buyer_id'] = $order_info['uid'];
                    $incomedata['income'] = $income;
                    $incomedata['return_percent'] = $return_percent;
                    $incomedata['percentremark'] = $order_info['total'] . '% x' . $percent . '%';
                    $incomedata['order_total'] = $order_info['total'];
                    $incomedata['pay_time'] = $order_info['pay_time'];
                    $incomedata['create_time'] = time();
                    $incomedata['month_time'] = date('m', time());
                    $incomedata['year_time'] = date('Y', time());
                    $incomedata['order_status_id'] = Config::getconfig()['paid_order_status_id'];

                    TechnicalIncomelog::create($incomedata);
                }
            }
        }
    }
}
